import { Graph } from 'graphlib';
import { Module } from './module';
import { ResolveType } from '../compiler';

export class ModuleGraph {
  graph: Graph;

  constructor() {
    this.graph = new Graph();
  }

  getModule(id: string): Module {
    return this.graph.node(id);
  }

  hasModule(id: string) {
    return !!this.graph.hasNode(id);
  }

  addModule(module: Module) {
    return this.graph.setNode(module.id, module);
  }

  getModules() {
    return this.graph.nodes();
  }

  addDependency(from: string, to: string, edge: Dependency) {
    return this.graph.setEdge(from, to, edge);
  }

  getDependencies(id: string) {
    return this.graph.outEdges(id);
  }
}

export interface Dependency {
  source: string;
  type: ResolveType;
}
